﻿using Microsoft.VisualBasic.FileIO;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DmTools.Model
{
    public class ItemInfoEntity
    {
        /// <summary>
        /// 找色语句文本
        /// </summary>
        public string ItemStr { get; set; }
        /// <summary>
        /// 语句参数信息
        /// </summary>
        [JsonIgnore]
        public ItemFieldInfoEntity ItemFieldInfo { get; set; }= new ItemFieldInfoEntity();
        /// <summary>
        /// 分解后的参数列表
        /// </summary>
        [JsonIgnore]
        public List<string> FieldItems { get; set; } = new List<string>();
        /// <summary>
        /// 语句参数数量
        /// </summary>
        public int ItemFiledCount { get; set; }
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="colorCast"></param>
        public void Init( string colorCast = "")
        {
            if (ItemStr.Contains(","))
            {
                FieldItems.Clear();
                using (var parser = new TextFieldParser(new System.IO.StringReader(ItemStr)))
                {
                    parser.TextFieldType = FieldType.Delimited;
                    parser.SetDelimiters(",");
                    parser.HasFieldsEnclosedInQuotes = true; // 关键：识别引号内的内容
                    FieldItems.AddRange(parser.ReadFields()); // 可选：去除空格
                }
            }
            ItemFiledCount = FieldItems.Count;
            if (!string.IsNullOrEmpty(colorCast))  //使用自带的偏色
            {
                FieldItems[4] = FieldItems[4].Split('-')[0] + "-" + colorCast;
                var offsetColos = FieldItems[5].Split(',');
                string newStr = "";
                foreach (var item in offsetColos)
                {
                    var spStrs = item.Split('|');
                    newStr += string.IsNullOrEmpty(newStr) ? "" : ",";
                    if (spStrs.Length == 4)
                    {
                        newStr += spStrs[0] + "|" + spStrs[1] + "|" + spStrs[2] + "|" + colorCast;
                    }
                    else
                    {
                        newStr += item;
                    }
                }
                FieldItems[5] = newStr;
            }
            if (FieldItems.Count == 11)
            {
                ItemFieldInfo.X1 = Convert.ToInt32(FieldItems[0]);
                ItemFieldInfo.Y1 = Convert.ToInt32(FieldItems[1]);
                ItemFieldInfo.X2 = Convert.ToInt32(FieldItems[2]);
                ItemFieldInfo.Y2 = Convert.ToInt32(FieldItems[3]);
                ItemFieldInfo.ColorFirst = FieldItems[4];
                ItemFieldInfo.ColorOffset = FieldItems[5];
                ItemFieldInfo.Sim = Math.Round(Convert.ToDouble(FieldItems[6]),3);
                ItemFieldInfo.PreDelay = Convert.ToInt32(FieldItems[7]);
                ItemFieldInfo.PostDelay= Convert.ToInt32(FieldItems[8]);
                ItemFieldInfo.IsCount = Convert.ToInt32(FieldItems[9]);
                ItemFieldInfo.LogStr = FieldItems[10];
            }
            else if (FieldItems.Count == 15)
            {
                ItemFieldInfo.X1 = Convert.ToInt32(FieldItems[0]);
                ItemFieldInfo.Y1 = Convert.ToInt32(FieldItems[1]);
                ItemFieldInfo.X2 = Convert.ToInt32(FieldItems[2]);
                ItemFieldInfo.Y2 = Convert.ToInt32(FieldItems[3]);
                ItemFieldInfo.ColorFirst = FieldItems[4];
                ItemFieldInfo.ColorOffset = FieldItems[5];
                ItemFieldInfo.XX1 = Convert.ToInt32(FieldItems[6]);
                ItemFieldInfo.YY1 = Convert.ToInt32(FieldItems[7]);
                ItemFieldInfo.XX2= Convert.ToInt32(FieldItems[8]);
                ItemFieldInfo.YY2 = Convert.ToInt32(FieldItems[9]);
                ItemFieldInfo.Sim = Math.Round(Convert.ToDouble(FieldItems[10]), 3);
                ItemFieldInfo.PreDelay = Convert.ToInt32(FieldItems[11]);
                ItemFieldInfo.PostDelay = Convert.ToInt32(FieldItems[12]);
                ItemFieldInfo.IsCount = Convert.ToInt32(FieldItems[13]);
                ItemFieldInfo.LogStr = FieldItems[14];
               
            }
        }
    }
    //x1,y1,x2,y2,fistr,offstr,sim,time1,time2,log
    //x1,y1,x2,y2,fistr,offstr,xx1,yy1,xx2,yy2,sim,time1,time2,log
    /// <summary>
    /// 语句参数信息
    /// </summary>
    public class ItemFieldInfoEntity
    {
        public int X1 { get; set; } = -1;
        public int X2 { get; set; } = -1;
        public int Y1 { get; set; } = -1;
        public int Y2 { get; set; } = -1;
        public string ColorFirst { get; set; }
        public string ColorOffset { get; set; }
        public double Sim { get; set; } = 1;
        public int PreDelay { get; set; }
        public int PostDelay { get; set; }
        public string LogStr { get; set; }
        public int XX1 { get; set; } = -1;
        public int XX2 { get; set; } = -1;
        public int YY1 { get; set; } = -1;
        public int YY2 { get; set; } = -1;
        public int IsCount { get; set; }
    }

    }
